package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.checkutils.CheckInsertResultUtil;
import com.xuecheng.content.model.dto.MediaFilesDto;
import com.xuecheng.content.model.po.TeachplanMedia;
import com.xuecheng.content.mapper.TeachplanMediaMapper;
import com.xuecheng.content.service.TeachplanMediaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Lang;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class TeachplanMediaServiceImpl extends ServiceImpl<TeachplanMediaMapper, TeachplanMedia> implements TeachplanMediaService {

    @Autowired
    TeachplanMediaMapper teachplanMediaMapper;

    //跟新文件信息
    //Map<String, MediaFilesDto>  String旧文件ID  MediaFileDto 新文件信息
    @Override
    public Boolean updateFileInfo(Map<String, MediaFilesDto> mediaFilesMap) {
        String firstKey = mediaFilesMap.keySet().iterator().next();
        TeachplanMedia teachplanMedia = teachplanMediaMapper.selectOne(new LambdaQueryWrapper<TeachplanMedia>().eq(TeachplanMedia::getMediaId,firstKey));
        if(teachplanMedia != null){
            MediaFilesDto mediaFilesDto = mediaFilesMap.get(firstKey);
            teachplanMedia.setMediaId(mediaFilesDto.getFileId());
            teachplanMedia.setMediaFilename(mediaFilesDto.getFilename());
            int i = teachplanMediaMapper.updateById(teachplanMedia);
            if(i>0){
                return true;
            }
            return false;
        }
        return false;
    }

    @Override
    public List<TeachplanMedia> selectListByCourseId(String courseId) {
        return teachplanMediaMapper.selectList(new LambdaQueryWrapper<TeachplanMedia>().eq(TeachplanMedia::getCourseId,courseId));
    }
}
